/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.bandwidth;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.SysCommandExecutor;

/**
 *
 * @author Administrator
 */
public class FilterInfo {
    String sourceAdd, destAdd, sourceSubnet, destSubnet, protocolName;
    int sourcePort, destPort;
    String description;
    boolean isActive;
    Timestamp time;
    long id;
    String INSERT_CMD = "INSERT INTO JNA_FILTER VALUES(?,?,?,?,?,?,?,?,?,?,?)";
    String DELETE_CMD = "DELETE FROM jNA_FILTER WHERE ID = ?";
    String UPDATE_CMD = "UPDATE JNA_FILTER SET IS_ACTIVE = ? WHERE ID = ?";

    public FilterInfo(String sourceAdd, String destAdd, String sourceSubnet,
            String destSubnet, String protocolName, int sourcePort,
            int destPort, String description, boolean isActive) {
        Date currentTime = new Date();
        this.time = new Timestamp(currentTime.getTime());
        this.sourceAdd = sourceAdd;
        this.destAdd = destAdd;
        this.sourceSubnet = sourceSubnet;
        this.destSubnet = destSubnet;
        this.protocolName = protocolName;
        this.sourcePort = sourcePort;
        this.destPort = destPort;
        this.description = description;
        this.isActive = isActive;
        this.id = this.time.getTime();
    }

    public FilterInfo(String sourceAdd, String destAdd, String sourceSubnet,
            String destSubnet, String protocolName, int sourcePort,
            int destPort, String description, boolean isActive, Timestamp timestamp) {
        this.sourceAdd = sourceAdd;
        this.destAdd = destAdd;
        this.sourceSubnet = sourceSubnet;
        this.destSubnet = destSubnet;
        this.protocolName = protocolName;
        this.sourcePort = sourcePort;
        this.destPort = destPort;
        this.description = description;
        this.isActive = isActive;
        this.time = timestamp;
        this.id = time.getTime();
    }
    public void InsertFilter(){
        Connection con = Program.connection;
        PreparedStatement pst = null;
        try {
            pst = con.prepareStatement(INSERT_CMD);
            pst.setLong(1, this.id);
            pst.setTimestamp(2, this.time);
            pst.setString(3, sourceAdd);
            pst.setString(4, sourceSubnet);
            pst.setString(5, destAdd);
            pst.setString(6, destSubnet);
            pst.setString(7, protocolName);
            pst.setInt(8, sourcePort);
            pst.setInt(9, destPort);
            pst.setBoolean(10, isActive);
            pst.setString(11, description);
            pst.executeUpdate();
            pst.close();
        } catch (SQLException ex) {
        }
        String cmd = getFilterString(this.isActive, false);
        SysCommandExecutor executor = new SysCommandExecutor();
        try {
            executor.runCommand(cmd);
        } catch (Exception ex) {
        }
    }
    public void RemoveFilter(){
        Connection con = Program.connection;
        PreparedStatement pst = null;
        try {
            pst = con.prepareStatement(DELETE_CMD);
            pst.setLong(1, this.id);
            pst.executeUpdate();
            pst.close();
        } catch (SQLException ex) {
        }
        this.isActive = false;
        String cmd1 = getFilterString(this.isActive, false);
        String cmd2 = getFilterString(this.isActive, true);
        SysCommandExecutor executor = new SysCommandExecutor();
        try {
            executor.runCommand(cmd1);
            executor.runCommand(cmd2);
        } catch (Exception ex) {
        }
    }
    private String getFilterString(boolean isAssign, boolean isDelete){
        StringBuilder cmd = new StringBuilder("IPSeccmd.exe");
        cmd.append(" -w REG -p ");
        cmd.append(String.valueOf(id));
        cmd.append(" -r ");
        cmd.append(String.valueOf(id));
        cmd.append(" -f ");
        if(!sourceAdd.equals("0")&& !sourceAdd.equals("*")){
            cmd.append(sourceAdd);
            cmd.append("/");
            cmd.append(sourceSubnet);
        } else {
            cmd.append(sourceAdd);
        }
        if(sourcePort != 0) cmd.append(":" + sourcePort);
        cmd.append("=");
        if(!destAdd.equals("0")&& !destAdd.equals("*")){
            cmd.append(destAdd);
            cmd.append("/");
            cmd.append(destSubnet);
        } else {
            cmd.append(destAdd);
        }
        if(destPort != 0) cmd.append(":" + destPort);
        if(!protocolName.equals("0")) cmd.append(":" + protocolName);
        cmd.append(" -n BLOCK");
        if(isDelete){
            cmd.append(" -o");
        } else {
            if(isAssign){
                cmd.append(" -x");
            } else {
                cmd.append(" -y");
            }
        }
        return cmd.toString();
    }
    
    public void ActivateOrDeactivate(){
        this.setIsActive(!isActive);
        Connection con = Program.connection;
        PreparedStatement pst = null;
        try {
            pst = con.prepareStatement(UPDATE_CMD);
            pst.setBoolean(1, this.isActive);
            pst.setLong(2, this.id);
            pst.executeUpdate();
            pst.close();
        } catch (SQLException ex) {
        }
        String cmd = getFilterString(this.isActive, false);
        SysCommandExecutor executor = new SysCommandExecutor();
        try {
            executor.runCommand(cmd);
        } catch (Exception ex) {
        }
    }
    public boolean getIsActive(){
        return isActive;
    }

    public String getDescription() {
        return description;
    }

    public String getDestAdd() {
        return destAdd;
    }

    public int getDestPort() {
        return destPort;
    }

    public String getDestSubnet() {
        return destSubnet;
    }

    public String getProtocolName() {
        return protocolName;
    }

    public String getSourceAdd() {
        return sourceAdd;
    }

    public int getSourcePort() {
        return sourcePort;
    }

    public String getSourceSubnet() {
        return sourceSubnet;
    }

    public boolean isIsActive() {
        return isActive;
    }

    public void setIsActive(boolean isActive) {
        this.isActive = isActive;
    }

    public long getId() {
        return id;
    }

    public Timestamp getTime() {
        return time;
    }





}
